package com.bocai.user.api;

import com.bocai.Response;
import com.bocai.ResponseCode;
import com.bocai.aspect.LoginChecker;
import com.bocai.user.model.User;
import com.bocai.user.model.vo.AuthInfo;
import com.bocai.user.service.UserService;
import com.bocai.utils.HashUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户controller
 * @author create by VIC.Huang
 * @date 2018/1/24
 */
@RestController
@RequestMapping("/user")
public class UserApi {
    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param username
     * @param passwd
     * @param fromuid
     * @return
     */
    @PostMapping("/register")
    public Response register(@RequestParam("username")String username, @RequestParam("passwd")String passwd,
                             @RequestParam(value = "fromuid", required = false, defaultValue = "0")Long fromuid) {
        //检测用户名是否存在
        User user = userService.getUserByName(username);
        if(null != user) {
            return new Response(ResponseCode.USER_EXISTENT.getValue(), ResponseCode.USER_EXISTENT.getDesc());
        }

        user = new User();
        user.setFromuid(fromuid).setNickName(username).setPasswd(HashUtils.getMD5(passwd));
        user.setWxHeadImg("/head.jpeg");
        if(userService.registerUser(user)) {
            return new Response(ResponseCode.SUCCESS.getValue(), ResponseCode.SUCCESS.getDesc());
        }else {
            return new Response(ResponseCode.SERVER_ERROR.getValue(), ResponseCode.SERVER_ERROR.getDesc());
        }
    }

    /**
     * 用户登录
     * @param username
     * @param passwd
     * @return
     */
    @PostMapping("/login")
    public Response login(@RequestParam("username")String username, @RequestParam("passwd")String passwd) {
        User user = userService.getUserByName(username);
        if(null != user) {
            if(user.getPasswd().equals(HashUtils.getMD5(passwd))) {
                if(user.getDisable()) {
                    return new Response(ResponseCode.USER_DISABLE.getValue(), ResponseCode.USER_DISABLE.getDesc());
                }
                String token = userService.saveLoginInfo(user);
                AuthInfo authInfo = new AuthInfo();
                authInfo.setUserId(user.getId()).setHeadImgUrl(user.getWxHeadImg())
                        .setNickname(user.getNickName()).setToken(token);
                return new Response(authInfo);
            }
        }
        return new Response(ResponseCode.LOGIN_FAILED.getValue(), ResponseCode.LOGIN_FAILED.getDesc());
    }

    /**
     * 查询用户下注记录
     * @param userId
     * @param type  下注记录类型  1: 所有  2：中奖  3：未中奖，失败
     * @return
     */
    @GetMapping("/betRecords")
    @LoginChecker
    public Response betRecords(@RequestParam("userId") Long userId, @RequestParam(value = "type", defaultValue = "1") Integer type,
                               @RequestParam(value = "page", defaultValue = "1") Integer page) {
        return new Response(userService.getBetRecords(userId, type, page));
    }

    /**
     * 用户账户信息（累计获胜/余额/今日参与/今日获胜/今日失败）
     * @param userId
     * @return
     */
    @GetMapping("/userAccountInfo")
    @LoginChecker
    public Response userAccountInfo(@RequestParam("userId") Long userId) {
        return new Response(userService.getUserAccountInfo(userId));
    }

    /**
     * 钱包明细
     * @param userId
     * @return
     */
    @GetMapping("/walletDetail")
    @LoginChecker
    public Response walletDetail(@RequestParam("userId") Long userId,
                                 @RequestParam(value = "page", required = false, defaultValue = "1") Integer page) {
        return new Response(userService.walletDetail(userId, page));
    }

    /**
     * 兑换记录
     * @param userId
     * @return
     */
    @GetMapping("/withdrawRecords")
    @LoginChecker
    public Response withdrawRecords(@RequestParam("userId") Long userId,
                                    @RequestParam(value = "page", required = false, defaultValue = "1") Integer page) {
        return new Response(userService.withdrawRecords(userId, page));
    }

    /**
     * 提现
     * @param token
     * @param money
     * @return
     */
    @PostMapping("/withdraw")
    @LoginChecker
    public Response withdraw(@RequestHeader("token")String token, @RequestParam("money")int money,
                             @RequestParam("qrcode")String qrcode) {
        try {
            return new Response(userService.withdraw(token, money, qrcode));
        } catch (Exception e) {
            return new Response(ResponseCode.SERVER_ERROR.getValue(), e.getMessage());
        }
    }

    /**
     * 佣金收益
     * @param token
     * @return
     */
    @GetMapping("/commissionProfit")
    @LoginChecker
    public Response commissionProfit(@RequestHeader("token")String token) {
        return new Response(userService.commissionProfit(token));
    }

    /**
     * 佣金历史收益
     * @param token
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/commissionRecords")
    @LoginChecker
    public Response commissionRecords(@RequestHeader("token")String token,
                                      @RequestParam(value = "page", defaultValue = "1")Integer page,
                                      @RequestParam(value = "pageSize", required = false, defaultValue = "10")Integer pageSize) {
        return new Response(userService.commissionRecords(token, page, pageSize));
    }

    /**
     * 反水历史记录
     * @param token
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/paybackRecords")
    @LoginChecker
    public Response paybackRecords(@RequestHeader("token")String token,
                                   @RequestParam(value = "page", defaultValue = "1")Integer page,
                                   @RequestParam(value = "pageSize", required = false, defaultValue = "10")Integer pageSize) {
        return new Response(userService.paybackRecords(token, page, pageSize));
    }

    /**
     * 收益来源玩家列表(不做分页)
     * @param token
     * @return
     */
    @GetMapping("/commissionPlayerList")
    @LoginChecker
    public Response commissionPlayerList(@RequestHeader("token")String token) {
        return new Response(userService.commissionPlayerList(token));
    }
}
